package com.cloud.item.controller;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import com.netflix.hystrix.contrib.javanica.conf.HystrixPropertiesManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.Map;

/**
 * @author lscl
 * @version 1.0
 * @intro:
 */
//@RestController
//@RequestMapping("/item")
public class ItemController_信号量隔离_Bak {

    @Autowired
    private RestTemplate restTemplate;


    /**
     * 测试Hystrix接口
     *
     * @param flag
     * @return
     */
    @HystrixCommand(fallbackMethod = "fallBack")            // 降级方法
    @GetMapping("/testHystrix/{flag}")
    public Map findOrderById(@PathVariable String flag) {

        Map resultMap = restTemplate.getForObject("http://order-service/order/testHystrix/" + flag, Map.class);

        return resultMap;
    }

    // 降级方法
    public Map fallBack(String flag) {

        Map fallBackMap = new HashMap() {{
            put("flag", true);
            put("message", "触发降级方法！");
            put("statusCode", "400");
            put("flag", flag);
        }};

        return fallBackMap;
    }


    @HystrixCommand(
            commandProperties = {
                    @HystrixProperty(
                            // 超时时间,默认为1000ms
                            name = "execution.isolation.thread.timeoutInMilliseconds",
                            value = "10000"
                    ),
                    // 隔离策略 默认为Thread(线程池隔离) SEMAPHORE:信号量隔离
                    @HystrixProperty(name = HystrixPropertiesManager.EXECUTION_ISOLATION_STRATEGY, value = "SEMAPHORE"),
                    // 信号量最大并发
                    @HystrixProperty(name = HystrixPropertiesManager.EXECUTION_ISOLATION_SEMAPHORE_MAX_CONCURRENT_REQUESTS, value = "5"),
            },
            fallbackMethod = "findOrderByIdFallBack"             // 降级方法
    )
    @GetMapping("/findOrderById/{orderId}")
    public Map findOrderById(@PathVariable Integer orderId) {

        System.out.println(Thread.currentThread().getName());
        Map resultMap = restTemplate.getForObject("http://order-service/order/" + orderId, Map.class);

        return resultMap;
    }

    /**
     * findOrderById降级方法
     * @param flag
     * @return
     */
    public Map findOrderByIdFallBack(Integer flag) {

        System.out.println("触发降级方法....findOrderByIdFallBack");
        Map fallBackMap = new HashMap() {{
            put("flag", true);
            put("message", "触发降级方法！");
            put("statusCode", "400");
            put("flag", flag);
        }};

        return fallBackMap;
    }

    @HystrixCommand(
            commandProperties = {
                    @HystrixProperty(
                            // 超时时间,默认为1000ms
                            name = "execution.isolation.thread.timeoutInMilliseconds",
                            value = "10000"
                    ),
                    // 隔离策略 默认为Thread(线程池隔离) SEMAPHORE:信号量隔离
                    @HystrixProperty(name = HystrixPropertiesManager.EXECUTION_ISOLATION_STRATEGY, value = "SEMAPHORE"),
                    // 信号量最大并发
                    @HystrixProperty(name = HystrixPropertiesManager.EXECUTION_ISOLATION_SEMAPHORE_MAX_CONCURRENT_REQUESTS, value = "5"),
            },
            fallbackMethod = "findOrderAllFallBack"             // 降级方法
    )
    @GetMapping("/findOrderAll")
    public Map findOrderAll() {

        System.out.println(Thread.currentThread().getName());
        Map resultMap = restTemplate.getForObject("http://order-service/order/", Map.class);

        return resultMap;
    }

    /**
     * findOrderAll 降级方法
     * @return
     */
    public Map findOrderAllFallBack() {

        System.out.println("触发降级方法....findOrderAllFallBack");
        Map fallBackMap = new HashMap() {{
            put("flag", true);
            put("message", "触发降级方法！");
            put("statusCode", "400");
        }};

        return fallBackMap;
    }
}